using System;
using ConfOrm;
using ConfOrm.NH;
using DMS.BEs;
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;

namespace DMS.DAOs.NH.ConfOrm
{
    public static class Config
    {
        public static ObjectRelationalMapper GetMappedDomain()
        {
            ObjectRelationalMapper orm = new ObjectRelationalMapper();

            orm.TablePerClass<Album>();
            orm.TablePerClass<Artist>();
            orm.TablePerConcreteClass<Person>();
            orm.TablePerClass<Genre>();
            orm.TablePerClass<Invoice>();
            orm.TablePerClass<InvoiceLine>();
            orm.TablePerClass<MediaType>();
            orm.TablePerClass<Playlist>();
            orm.TablePerClass<Track>();

            orm.Bag<Playlist>(pl => pl.Tracks);
            orm.ManyToMany<Playlist, Track>();

            return orm;
        }

        public static Mapper GetMapper()
        {
            ObjectRelationalMapper orm = GetMappedDomain();
            Mapper mapper = new Mapper(orm);

            mapper.Class<Artist>(p => p.Property(be => be.Name, t => t.UniqueKey("Artist_Unique")));

            mapper.Class<Album>(p => p.ManyToOne(be => be.Artist, t => t.UniqueKey("Album_Unique")));
            mapper.Class<Album>(p => p.Property(be => be.Title, t => t.UniqueKey("Album_Unique")));

			//mapper.Class<Track>(p => p.ManyToOne(be => be.Album, t => t.UniqueKey("Track_Unique")));
			//mapper.Class<Track>(p => p.Property(be => be.Name, t => t.UniqueKey("Track_Unique")));

            mapper.Class<Person>(p => p.Property(be => be.FirstName, t => t.UniqueKey("Person_Unique")));
            mapper.Class<Person>(p => p.Property(be => be.LastName, t => t.UniqueKey("Person_Unique")));

            return mapper;
        }

        public static HbmMapping GetMapping()
        {
            Mapper mapper = GetMapper();

            Type type = typeof(Track);
            HbmMapping mapping = mapper.CompileMappingFor(type.Assembly.GetTypes());

            return mapping;
        }


        public static Configuration GetConfiguration()
        {
            Configuration configuration = new Configuration();
            configuration.Configure();

            HbmMapping mapping = GetMapping();
            configuration.AddDeserializedMapping(mapping, "DMS_BEs");

            BEs.Validations.NHV.Config.Configure(configuration);

            return configuration;
        }
    }
}
